package main

import "fmt"

func print[T any](arr []T) {
	for _, v := range arr {
		fmt.Print(v)
		fmt.Print(" ")
	}
	fmt.Println("")
}
func find[T comparable](arr []T, elem T) int {
	for i, v := range arr {
		if v == elem {
			return i
		}
	}
	return -1
}
func main() {
	//strs := []string{"Hello", "World", "Generics"}
	//decs := []float64{3.14, 1.14, 1.618, 2.718}
	//nums := []int{2, 4, 6, 8}
	//i := find(nums, 3)
	//fmt.Println(i)
	//print(strs)
	//print(decs)
	//print(nums)
}

// stack 泛型实现栈
type stack[T any] []T

func (s *stack[T]) push(elem T) {
	*s = append(*s, elem)
}

func (s *stack[T]) pop() {
	if len(*s) > 0 {
		*s = (*s)[:len(*s)-1]
	}
}
func (s *stack[T]) top() *T {
	if len(*s) > 0 {
		return &(*s)[len(*s)-1]
	}
	return nil
}

func (s *stack[T]) len() int {
	return len(*s)
}

func (s *stack[T]) print() {
	for _, elem := range *s {
		fmt.Print(elem)
		fmt.Print(" ")
	}
	fmt.Println("")
}
